﻿Imports Orekaria.Lib.Win32

Public Class Mdi
    Private WithEvents _timer As Timer
    Private _necesitaFlash As Boolean

    Private Sub MdiLoad(sender As Object, e As EventArgs) Handles MyBase.Load
        Text = Título
        Icon = Icon.FromHandle(My.Resources.H200.GetHicon())

#If Not Debug Then
        WindowState = FormWindowState.Maximized
#End If

    End Sub

    Private Sub MdiShown(sender As Object, e As EventArgs) Handles Me.Shown
        '#If Debug Then
        '        P2PEncapsulado.HostList.Add(New HostHelper("Txomin"))
        '        'P2PEncapsulado.HostList.Add(New HostHelper("Maite"))
        '        'P2PEncapsulado.HostList.Add(New HostHelper("Edurne"))
        '#Else
        Dim dlg = New FrmLauncher
        dlg.ShowDialog()
        '#End If
        P2PEncapsulado.EstablecePrimerTurno()

        For Each h As HostHelper In P2PEncapsulado.HostList
            CreaNuevoTablero(h)
        Next
        LayoutMdi(MdiLayout.TileVertical)

        _timer = New Timer()
        _timer.Interval = 100
        AddHandler _timer.Tick, AddressOf TimerTick
        _timer.Start()
    End Sub

    Private Sub MdiFormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        If Not _timer Is Nothing Then _timer.Stop()
        P2PEncapsulado.Dispose()
    End Sub

    Private Sub CreaNuevoTablero(h As HostHelper)
        Dim childForm = New FrmTablero(h)
        childForm.MdiParent = Me
        childForm.Show()
    End Sub

    Private Sub TimerTick(ByVal sender As Object, ByVal e As EventArgs)
        _timer.Stop()
        For Each child As FrmTablero In MdiChildren
            child.Ola()
        Next
        If _necesitaFlash Then
            If P2PEncapsulado.EsMiTurno Then
                WindowAction.Flash(Handle, _necesitaFlash)
                _necesitaFlash = False
            End If
        End If
        _timer.Start()
    End Sub

    Private Sub MdiActivated(sender As Object, e As System.EventArgs) Handles Me.Activated
        _necesitaFlash = False
        WindowAction.Flash(Handle, _necesitaFlash)
    End Sub

    Private Sub MdiDeactivate(sender As Object, e As System.EventArgs) Handles Me.Deactivate
        _necesitaFlash = True
    End Sub
End Class
